//
//  WBVisitorView.swift
//  weibo
//
//  Created by 李江波 on 2016/10/12.
//  Copyright © 2016年 lijiangbo. All rights reserved.
//

import UIKit

/// 访客试图
class WBVisitorView: UIView {
    
    //注册和登录按钮
     lazy var registerButton: UIButton = UIButton(title: "注册", sizeFont: 16, normalColor: UIColor.orange, highlightedColor: UIColor.lightGray, backgroundImageName: "common_button_white")
    
     lazy var loginButton: UIButton = UIButton(title: "登录", sizeFont: 16, normalColor: UIColor.black, highlightedColor: UIColor.lightGray, backgroundImageName: "common_button_white")
    
    //访客信息字典
    var visitorInfo : [String : String]? {
    
        didSet{
            guard let imageName = visitorInfo?["imageName"],
                let message = visitorInfo?["message"] else {
                    
                    return
            }
            tipLable.text = message
            
            if imageName == "" {
                
                setAnimation()
                return
            }
            
            iconImage.image = UIImage(named:imageName)
            
            houseIconView.isHidden = true
            maskIconView.isHidden = true
            
        }
    
    }
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        
       setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    /// 设置首页动画
    private func setAnimation() {
    
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = 2 * M_PI
        
        animation.repeatCount = MAXFLOAT
        
        animation.duration = 12
        
        //是回来时继续保持有动画
        animation.isRemovedOnCompletion = false
        
        iconImage.layer.add(animation, forKey: "小房子动画")
       
    }
    
    
    fileprivate lazy var iconImage = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    
    /// 背景遮盖图片
    fileprivate lazy var maskIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    fileprivate lazy var houseIconView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    fileprivate lazy var tipLable: UILabel = UILabel(title: "快来注册，走过路过，不要错过快来注册，走过路过，不要错过快来注册，走过路过，不要错过", sizefont: 14, color: UIColor.gray)
    
    
}

extension WBVisitorView {

    func setupUI()  {
        
        backgroundColor = UIColor(rgbValue: 0xEDEDED)
        
        addSubview(iconImage)
        addSubview(maskIconView)
        addSubview(houseIconView)
        addSubview(tipLable)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //使用苹果原生态布局
        for v in subviews {
            
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        let margin : CGFloat = 20
        
        //自动布局
        addConstraint(NSLayoutConstraint(item: iconImage, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: iconImage, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1.0, constant: -60))
        
        addConstraint(NSLayoutConstraint(item: houseIconView, attribute: .centerX, relatedBy: .equal, toItem: iconImage, attribute: .centerX, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: houseIconView, attribute: .centerY, relatedBy: .equal, toItem: iconImage, attribute: .centerY, multiplier: 1.0, constant: 0))
        
        //文字
        addConstraint(NSLayoutConstraint(item: tipLable , attribute: .centerX, relatedBy: .equal, toItem: iconImage, attribute: .centerX, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: tipLable, attribute: .top, relatedBy: .equal, toItem: iconImage, attribute: .bottom, multiplier: 1.0, constant: margin))
        
        addConstraint(NSLayoutConstraint(item: tipLable, attribute: .width, relatedBy: .equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1.0, constant: 236))
        
        //注册按钮
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .left, relatedBy: .equal, toItem: tipLable, attribute: .left, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .top, relatedBy: .equal, toItem: tipLable, attribute: .bottom, multiplier: 1.0, constant: margin))
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute , multiplier: 1.0, constant: 100))
        
        //登录按钮
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .right, relatedBy: .equal, toItem: tipLable, attribute: .right, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .top, relatedBy: .equal, toItem: tipLable, attribute: .bottom, multiplier: 1.0, constant: margin))
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute , multiplier: 1.0, constant: 100))
        
        //背景遮罩图片===练习VF布局
        let views = ["maskIconView":maskIconView,"registerButton":registerButton] as [String : Any]
        
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[maskIconView]-0-|",options: [],metrics: nil,views: views))
        
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[maskIconView]-10-[registerButton]", options: [], metrics: nil, views: views))
        
        
    }

}







